<template>
  <p-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-form-item label="申请编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'applyNo', validatorRules.applyNo]" placeholder="请输入申请编号"></p-input>
        </p-form-item>
        <p-form-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'createBy', validatorRules.createBy]" placeholder="请输入申请人"></p-input>
        </p-form-item>
        <p-form-item label="请假开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择请假开始时间" v-decorator="[ 'leaveStartTime', validatorRules.leaveStartTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="请假结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择请假结束时间" v-decorator="[ 'leaveEndTime', validatorRules.leaveEndTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </p-form-item>
        <p-form-item label="请假类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['leaveType', validatorRules.leaveType]" :trigger-change="true" dictCode="leave_type" placeholder="请选择请假类别"/>
        </p-form-item>
        <p-form-item label="请假原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'leaveReason', validatorRules.leaveReason]" placeholder="请输入请假原因"></p-input>
        </p-form-item>
        <p-form-item label="流程状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['bpmStatus', validatorRules.bpmStatus]" :trigger-change="true" dictCode="bpm_status" placeholder="请选择流程状态"/>
        </p-form-item>
        <p-form-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'sysOrgCode', validatorRules.sysOrgCode]" placeholder="请输入所属部门"></p-input>
        </p-form-item>
        <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
        </p-form-item>

      </p-form>
    </p-spin>
    <p-button type="primary" @click="handleOk">确定</p-button>
    <p-button type="primary" @click="handleCancel">取消</p-button>
  </p-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "LeaveApplyModal",
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          applyNo: {rules: [
          ]},
          createBy: {rules: [
          ]},
          leaveStartTime: {rules: [
            {required: true, message: '请输入请假开始时间!'},
          ]},
          leaveEndTime: {rules: [
            {required: true, message: '请输入请假结束时间!'},
          ]},
          leaveType: {rules: [
            {required: true, message: '请输入请假类别!'},
          ]},
          leaveReason: {rules: [
            {required: true, message: '请输入请假原因!'},
          ]},
          bpmStatus: {rules: [
          ]},
          sysOrgCode: {rules: [
          ]},
          remark: {rules: [
          ]},
        },
        url: {
          add: '/codedemoapi/feixuns/leaveApply/add',
          edit: '/codedemoapi/feixuns/leaveApply/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'applyNo','createBy','createTime','leaveStartTime','leaveEndTime','leaveType','leaveReason','bpmStatus','sysOrgCode','remark'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.msg);
                that.$emit('ok');
              }else{
                that.$message.warning(res.msg);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'applyNo','createBy','createTime','leaveStartTime','leaveEndTime','leaveType','leaveReason','bpmStatus','sysOrgCode','remark'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>